/**
 * 房型定价新增表单
 */

define(function (require, exports, module) {
	var Form = require("form");
	var roomTypeDataSvc = require("../store/roomTypeDataSvc");
	var FormEvents = require("../handler/FormEvents");
	var moment = require("moment");
	var user = require("store").get("user");
	
    var instance = {
        initComponent: function (params, widget) {
        	
        	var form = new Form({
                parentNode: ".J-form",
                model: {
                    id: "form",
                    saveaction: function() {
                        var data = form.getData();
                        FormEvents.saveAction(data, widget);
                    },
                    saveText: "保存",
                    cancelaction: function() {
                        widget.get("form").reset();
                        widget.hide([".J-form"]);
    	                
    	                var subnav = widget.get("subnav");
    	                subnav.hide(["return"]).show(["add","servicepoint","buiding","roomtypes"]);
    	                widget.get("monthRange").show();
    	                $(".J-calendar").show();
    	                widget.hide([".J-form"]);
                    },
                    cancelText: "取消",
                    items: [{
                        name: "servicePoint",
                        type: "select",
                        label: "所属服务点",
                        keyField :"pkServicePoint",
						valueField :"name",
						params:{
							fetchProperties:"pkServicePoint,name"
						},
						url:"api/servicepoint/operableservicepoints",
						items:user.privateServicePoints,
                        validate: ["required"],
                        events: {
                            "click": function(e) {
                               //查询楼宇
	   		                   roomTypeDataSvc.getOrgBuilding(e.target.value,function(buildingData){
	   		                	   form.setData("orgBuilding", buildingData);
	   		                   });
                            }
                        }
                    }, {
                        name: "orgBuilding",
                        label: "楼宇",
                        type: "select",
                        keyField: "pkOrgBuilding",
        	            valueField: "name",
        	            items:[],
                        validate: ["required"],
                        events: {
                            "click": function(e) {
                            	var servicePoint = form.getValue("servicePoint");
                            	//查询房型
   		                	    roomTypeDataSvc.getRoomtype(servicePoint,e.target.value,function(roomTypeData){
   		                		    form.setData("roomType", roomTypeData);
   		                        });
                            }
                        }
                    }, {
                        
                        name: "roomType",
                        label: "房型",
                        type: "select",
                        keyField: "pkRoomType",
        	            valueField: "name",
        	            items:[],
                        validate: ["required"]
                    },  {
                        name: "startDate",
                        label: "开始时间",
                        type: "date",
                        validate: ["required"],
                        minDate: new Date(moment().format("YYYY-MM-DD")),
                        events: {
                            "blur": function(e) {
                            	FormEvents.changeDate(form);
                            }
                        }
                    }, {
                        name: "endDate",
                        label: "结束时间",
                        type: "date",
                        validate: ["required"],
                        minDate: new Date(moment().format("YYYY-MM-DD")),
                        events: {
                            "blur": function(e) {
                            	FormEvents.changeDate(form);
                            }
                        },
                        exValidate: function(value) {
                        	var startDate = form.getValue("startDate");
                        	var endDate = form.getValue("endDate");
                        	if(startDate > endDate){
                        		return "开始时间不能大于结束时间";
                        	}else{
                        		return true;
                        	}
                        }
                    },{
                        name: "week",
                        type: "text",
                        label: "星期",
                        type: "checkbox",
                        list: [{
                            key: "Monday",
                            value: "星期一"
                        },{
                            key: "Tuesday",
                            value: "星期二"
                        },{
                            key: "Wednesday",
                            value: "星期三"
                        },{
                            key: "Thursday",
                            value: "星期四"
                        },{
                            key: "Friday",
                            value: "星期五"
                        },{
                            key: "Saturday",
                            value: "星期六"
                        },{
                            key: "Sunday",
                            value: "星期日"
                        }]
                    }, {
                        name: "vipPrice",
                        type: "text",
                        label: "会员价(元)",
                        validate: ["required"],
                        exValidate: function(value) {
                            if (!isNaN(value) && value>0) {
                                return true;
                            } else {
                            	return "输入的价格不合法";
                            }
                        }
                    }, {
                        name: "standardPrice",
                        type: "text",
                        label: "标准价(元)",
                        validate: ["required"],
                        exValidate: function(value) {
                            if (!isNaN(value) && value>0) {
                                return true;
                            } else {
                            	return "输入的价格不合法";
                            }
                        }
                    }, {
                        name: "protocolPrice",
                        type: "text",
                        label: "协议价(元)",
                        validate: ["required"],
                        exValidate: function(value) {
                            if (!isNaN(value) && value>0) {
                                return true;
                            } else {
                            	return "输入的价格不合法";
                            }
                        }
                    }]
                }
            });
            widget.set("form", form);
        	
        	
        }
    };
    module.exports = instance;
});
